package web.interceptors;

import org.springframework.web.servlet.HandlerInterceptor;
import pojo.Code;
import web.exhandlers.BusinessException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //判断用户是否登录，如果没有登录，不允许访问
        Object user = request.getSession().getAttribute("user");
        if(user == null){
            //没登陆

//            response.getWriter().write("NO_LOGIN");
//
//            return false;
            throw new BusinessException(Code.NO_LOGIN);
        }

        return true;
    }
}
